/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sale;

/**
 *
 * @author Yasmani
 */
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import search.Product;


public class Database {
   private static final String HOST = "localhost";
   private static final String PORT = "3306";
   private static final String DATABASE = "ismi";
   private static final String USER = "root";
   private static final String PASSWORD = "isaac16";

   private Connection connection;
   
   public Database() {}

   public void connect()
   {
      try
      {
         // Override the host if DB_SERVER_HOST is set.
         String host = System.getenv("DB_SERVER_HOST");
         if (host == null) host = HOST;

         Class.forName("com.mysql.jdbc.Driver");

         String url =
            "jdbc:mysql://" + host + ":" + PORT + "/" + DATABASE;
         connection =
            DriverManager.getConnection(url, USER, PASSWORD);
      }
      catch (Exception e) { System.err.println(e); }
   }

   public void disconnect()
   {
      try { connection.close(); }
      catch (Exception e) { System.err.println(e); }
   }
   /*
   public String LastProdID()
   {
       String lastProdID = "0";
       try
       {
           PreparedStatement statement = connection.prepareStatement(
                "select max(product_id) from product");
           ResultSet resultSet = statement.executeQuery();
           
           while (resultSet.next())
           {
               lastProdID = resultSet.getString(1);
              
           }
           
       }
       catch (Exception e) { e.printStackTrace(); }
       
       return lastProdID;
   }
   
   */
   
   
   
   public void SaleProd(String id, String product_name, String product_description, String user_name, String pic, String price, String amount,
            String partNumber, String manufacturer)
   {
       Product saleProduct= new Product(id, product_name, product_description, user_name, pic, price, amount, partNumber,manufacturer);
       
//       int lastProdID = Integer.parseInt(LastProdID());
  //     lastProdID += 1;
    //   String lastProdIDs = Integer.toString(lastProdID);
      // System.out.print(lastProdIDs);
      
       
       try
       {
           //PreparedStatement statement =  connection.prepareStatement("INSERT INTO `ismi`.`product` VALUES (?, ?, ?, NULL, ?, ?, ?,?);");
           PreparedStatement statement =  connection.prepareStatement("INSERT INTO `ismi`.`product_pictures` VALUES (?);");
           statement.setString(1, saleProduct.getPic());
           statement.executeUpdate();
           PreparedStatement statement1 =  connection.prepareStatement("select  product_pictures_id from `ismi`.`picture` where product_pictures=(?);");
           statement1.setString(1, saleProduct.getPic());
           ResultSet resultSet1 = statement.executeQuery();
           int pid;
           while(resultSet1.next())
                   {
                       pid=resultSet1.getInt("product_pictures_id"); 
                  
           
           PreparedStatement statement2 =  connection.prepareStatement("INSERT INTO `ismi`.`product` VALUES (?, ?, ?,  ?, ?, ?,?);");
           //statement.setString(1, lastProdIDs);
           statement.setString(1, saleProduct.getProduct_name());
           statement.setString(2, saleProduct.getProduct_description());
           statement.setInt(3, pid);
           statement.setFloat(4, Float.parseFloat( saleProduct.getPrice()));
           statement.setInt(5, Integer.parseInt(saleProduct.getAmount()));
           statement.setString(6, saleProduct.getPartNumber());
           statement.setString(7, saleProduct.getManufacturer());
           statement.executeUpdate();
           
                   } 
       }
       catch (Exception e) {e.printStackTrace();}
       
   }
   
   //for testing
   /*
   public static void main(String[] args)
   {
      Database database = new Database();
      database.connect();
      database.SaleProd();
      database.disconnect();
   }
   */ 

    
}
